package com.hzit.test08;

import java.util.Scanner;

/**
 * 作者：Dzm
 * 时间：2024-07-06-10:13
 * 功能：
 */
public class BookUI {
    private Book[] books = new Book[10];
    private int count = 0;
    private Scanner scanner = new Scanner(System.in);;

    {
        books[count++] = new Book(1001, "西游记", 23.8, "北京出版社", 3);
    }

    public void showUI() {
        System.out.println("\n\t\t\t =_____________________=");
        System.out.println("=============[_____________________]==============");
        System.out.println("||||||||||||||***欢迎光临图书系统***||||||||||||||||");
        System.out.println(" [@]=========|—————————————————————|==========[@]");
        System.out.println(" [@] |\t|\t|\t\t1.添加图书\t\t|\t|\t| [@]");
        System.out.println(" [@] |\t|\t|\t\t2.查看图书\t\t|\t|\t| [@]");
        System.out.println(" [@] |\t|\t|\t\t3.查找图书\t\t|\t|\t| [@]");
        System.out.println(" [@] |\t|\t|\t\t4.退    出\t\t|\t|\t| [@]");
        System.out.println(" [@] |\t________________________________\t| [@]");
        System.out.println(" [@] |\t[_______________________________]\t| [@]");
        System.out.println(" [@] ￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥[@]");
        System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
    }

    public void userSelect() {
        System.out.print("请输入选项（1-4）：");
        int choice = scanner.nextInt();
        System.out.println();

        switch (choice) {
            case 1:
                addBook();
                break;
            case 2:
                readBooks();
                break;
            case 3:
                searchBooks();
                break;
            case 4:
                System.out.println("\n退出成功！\n");
                System.exit(0);
                break;
            default:
                System.out.println("\n请正确输入选项！\n");
                break;
        }

        System.out.print("是否继续？(y/n)：");
        String answer = scanner.next();
        if(answer.equalsIgnoreCase("y")) {
            userSelect();
        }
    }

    private void addBook() {
        System.out.print("请输入图书名称：");
        String bname = scanner.next();
        System.out.print("请输入图书单价：");
        double price = scanner.nextDouble();
        System.out.print("请输入出版社：");
        String publisher = scanner.next();
        System.out.print("请输入购买数量：");
        int num = scanner.nextInt();

        if(count < 10) {
            Book book = new Book(1001 + count, bname, price, publisher, num);
            books[count++] = book;
            System.out.println("\n图书添加成功！\n");
        } else {
            System.out.println("\n库存已满，无法添加！\n");
        }

    }

    private void readBooks() {
        double sum = 0;
        System.out.println("图书编号\t图书名称\t图书单价\t出版社\t\t购买数量\t小计");
        System.out.println("---------------------------------------------------------------");
        for (int i = 0; i < count; i++) {
            sum += books[i].price*books[i].num;
            System.out.println(books[i]);
        }

        System.out.println("\n价格总计：" + sum);
        System.out.println();
    }

    private void searchBooks() {
        System.out.println("查找图书中。。。。");
    }
}
